const simulateAsyncOperation = fn => {
  setTimeout(fn, 2000)
}
const data = function() {
  return {
    projectList: [],
    distEngineeringTypeList: [],
    leveList: [],
    riskCategoryList: [],
    constructionStateList: [], //施工状态
    //engineeringTypeList: [], //工程类型
    proTypeList: [], //项目类型
    projectId: '',
    wbsList: [],
    ledgerObj: {
      code:''
    }
  }

}
const methods = {
  //风险类别
  getRiskCategoryFindAll() {
    this.$api.measuresLibrary.getRiskCategoryFindAll().then(res => {
      if (res.code == 200) {
        this.riskCategoryList = res.data
      } else {
        this.$Message.error(res.message)
      }
    })
  },
  // 风险等级
  dictTypeList(code) {
    this.$api.UnitProject.getListByTypeCode(code).then(resp => {
      if (resp.code == 200) {
        this.leveList = resp.data
      } else {
        this.$Message.warning(resp.message)
      }
    })
  },
  //获取施工状态
  getConstructionState(code) {
    this.$api.projectInquiry.getListByTypeCode(code).then(res => {
      if (res.code == 200) {
        this.constructionStateList = res.data
      } else {
        this.$Message.error(res.data.msg)
      }
    })
  },
  //获取项目类型
  getProType() {
    let code = 'PROJECT_TYPE'
    this.$api.projectInquiry.getListByTypeCode(code).then(res => {
      if (res.code == 200) {
        this.proTypeList = res.data
      } else {
        this.$Message.error(res.data.msg)
      }
    })
  },
  
  // 项目列表
  projectGetAll() {
    let parmes = {
      cascade:true,
      isQueryJurProject:true
    }
    this.$api.WorkstationLedger.projectGetAll(parmes).then(resp => {
      if (resp.code == 200) {
        this.projectList = resp.data
      } else {
        this.$Message.warning(resp.message)
      }
    })
  },
  selectProject(val) {
    this.ledgerObj.projectId = val
  },
  getCodeByProjectId(){
    let params = {
        projectId : this.ledgerObj.projectId
    }
    this.$api.WorkstationLedger.codeByProjectId(params).then(res => {
        if (res.code == 200) {
          this.ledgerObj.code = res.data
        } else {
            this.$Message.warning(res.message)
        }
    })
},
  projectTypeList(value) {
    if (this.ledgerObj.projectId) {
      this.getCodeByProjectId()
      let res = {
        currentPage: 1,
        pageSize: 99999,
        whereMap: {
          projectId: this.ledgerObj.projectId,
          parentId: 0
        }
      }
      this.$api.WorkstationLedger.professionalTypeList(res).then(res => {
        if (res.code == 200) {
          res.data.resultObject.map(r => {
            this.$set(r, 'title', r.name)
            if (r.hasChildren) {
              this.$set(r, 'children', [])
              this.$set(r, 'loading', false)
            }
          })
          this.wbsList = [
            {
              title: '标准WBS',
              parentId: 0,
              hasChildren: false,
              loading: false,
              expand: true,
              children: res.data.resultObject
            }
          ]
        } else {
          this.$Message.error(res.data.msg)
        }
      })
    } else {
      if (value) {
        this.$Message.warning('请先选择项目名称')
      }
    }
  },
  loadData(item, callback) {
    let params = {
      currentPage: 1,
      pageSize: 99999,
      whereMap: {
        projectId: this.ledgerObj.projectId,
        parentId: item.id
      }
    }
    let data = []
    this.$api.WorkstationLedger.professionalTypeList(params).then(res => {
      if (res.code == 200) {
        callback(this.getTree(res.data.resultObject))
      } else {
        this.$Message.error(res.data.msg)
      }
    })
  },
  getTree(tree) {
    return tree.map(node => {
      node.title = node.name
      if (node.hasChildren) {
        node.loading = false
        node.children = []
      }
      return node
    })
  }

  // 专业懒加载
  // loadOptions({ action, parentNode, callback }) {
  //   if (parentNode.hasChildren) {
  //     simulateAsyncOperation(() => {
  //       let parmes = {
  //         whereMap: {
  //           projectId: this.projectId,
  //           parentId: parentNode.id
  //         }
  //       }
  //       this.$api.WorkstationLedger.professionalTypeList(parmes).then(resp => {
  //         if (resp.code == 200) {
  //           resp.data.resultObject.forEach(e => {
  //             e.label = e.name
  //             if (e.hasChildren) {
  //               e.loading = false
  //               e.children = null
  //             }
  //           })
  //           parentNode.children = resp.data.resultObject
  //         } else {
  //           this.$Message.warning(resp.message)
  //         }
  //       })
  //       callback()
  //     })
  //   }
  // }
  //获取工程类型
  // getEngineeringType() {
  //   let code = 'ENGINEERING_TYPE'
  //   this.$api.projectInquiry.getTreeByTypeCode(code).then(res => {
  //     if (res.code == 200) {
  //       //this.engineeringTypeList = res.data
  //       res.data.forEach(e => {
  //         e.label = e.name
  //         if (e.hasChildren) {
  //           e.loading = false
  //           e.children = null
  //         }
  //       })
  //       this.engineeringTypeList = res.data
  //     } else {
  //       this.$Message.error(res.data.msg)
  //     }
  //   })
  // },
  // loadOptionsEngineer({ action, parentNode, callback }) {
  //   console.log('parentNode',parentNode)
  //   if (parentNode.hasChildren) {
  //     simulateAsyncOperation(() => {
  //       let parmes = {
  //         whereMap: {
  //           projectId: this.projectId,
  //           parentId: parentNode.id
  //         }
  //       }
  //       this.$api.projectInquiry.getTreeByTypeCode(parmes).then(resp => {
  //         if (resp.code == 200) {
  //           resp.data.resultObject.forEach(e => {
  //             e.label = e.name
  //             if (e.hasChildren) {
  //               e.loading = false
  //               e.children = null
  //             }
  //           })
  //           parentNode.children = resp.data.resultObject
  //         } else {
  //           this.$Message.warning(resp.message)
  //         }
  //       })
  //       callback()
  //     })
  //   }
  // }
}

export default {
  methods,
  data
}
